package cn.iocoder.yudao.module.picturebook.controller.admin.classhomework;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.classhomework.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.classhomework.ClassHomeworkDO;
import cn.iocoder.yudao.module.picturebook.service.classhomework.ClassHomeworkService;

@Slf4j
@Tag(name = "绘本系统 - 班级作业信息")
@RestController
@RequestMapping("/picturebook/class-homework")
@Validated
public class ClassHomeworkController {

    @Resource
    private ClassHomeworkService classHomeworkService;

    @PostMapping("/create")
    @Operation(summary = "创建班级作业信息")
    @PreAuthorize("@ss.hasPermission('picturebook:class-homework:create')")
    public CommonResult<Long> createClassHomework(@Valid @RequestBody ClassHomeworkSaveReqVO createReqVO) {
        return success(classHomeworkService.createClassHomework(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级作业信息")
    @PreAuthorize("@ss.hasPermission('picturebook:class-homework:update')")
    public CommonResult<Boolean> updateClassHomework(@Valid @RequestBody ClassHomeworkSaveReqVO updateReqVO) {
        classHomeworkService.updateClassHomework(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级作业信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:class-homework:delete')")
    public CommonResult<Boolean> deleteClassHomework(@RequestParam("id") Long id) {
        classHomeworkService.deleteClassHomework(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级作业信息")
                @PreAuthorize("@ss.hasPermission('picturebook:class-homework:delete')")
    public CommonResult<Boolean> deleteClassHomeworkList(@RequestParam("ids") List<Long> ids) {
        classHomeworkService.deleteClassHomeworkListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级作业信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:class-homework:query')")
    public CommonResult<ClassHomeworkRespVO> getClassHomework(@RequestParam("id") Long id) {
        ClassHomeworkDO classHomework = classHomeworkService.getClassHomework(id);
        return success(BeanUtils.toBean(classHomework, ClassHomeworkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级作业信息分页")
    @PreAuthorize("@ss.hasPermission('picturebook:class-homework:query')")
    public CommonResult<PageResult<ClassHomeworkRespVO>> getClassHomeworkPage(@Valid ClassHomeworkPageReqVO pageReqVO) {
        PageResult<ClassHomeworkDO> pageResult = classHomeworkService.getClassHomeworkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassHomeworkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级作业信息 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:class-homework:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassHomeworkExcel(@Valid ClassHomeworkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassHomeworkDO> list = classHomeworkService.getClassHomeworkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级作业信息.xls", "数据", ClassHomeworkRespVO.class,
                        BeanUtils.toBean(list, ClassHomeworkRespVO.class));
    }

    //根据班级id去查询班级作业信息
    @GetMapping("/get-by-class-id")
    @Operation(summary = "根据班级id和当前用户去查询班级作业信息")
    public CommonResult<List<ClassHomeWorkDataRespVO>> getClassHomeworkByClassId(@RequestParam("classId") Long classId, @RequestHeader("token") Long userId) {
        // 校验 userId 合法性
        if (userId == null || userId <= 0) {
            return CommonResult.error(303, "用户ID无效");
        }

        List<ClassHomeWorkDataRespVO> list = classHomeworkService.getClassHomeworkByClassIdAndUserId(classId, userId);
        return CommonResult.success(list);
    }

    //获取作业完成情况
    @GetMapping("/get-complete-ratio")
    @Operation(summary = "获取作业完成情况")
    public CommonResult<ClassHomeWorkDetailRespVO> getCompleteRatio(@RequestParam("classId") Long classId,@RequestParam("workId") Long workId, @RequestHeader("token") Long userId) {
        // 校验 userId 合法性
        if (userId == null || userId <= 0) {
            return CommonResult.error(303, "用户ID无效");
        }
        ClassHomeWorkDetailRespVO detail = classHomeworkService.getCompleteRatio(classId,workId,userId);
        return CommonResult.success(detail);
    }
}